package com.vaadin.thomas.contenttool.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;

import com.vaadin.thomas.contenttool.data.Localization;
import com.vaadin.thomas.contenttool.data.Value;
import com.vaadin.thomas.contenttool.data.Version;
import com.vaadin.thomas.contenttool.ui.events.VersionChangedEvent;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

public class VersionInfoWindow extends Window {

	private static final long serialVersionUID = -6734271993470345506L;

	private Version version;

	private Button showAll;

	private Button clearAll;

	private DateFormat df = new SimpleDateFormat("HH:mm dd.MM.yyyy");

	public VersionInfoWindow(Version version) {

		this.version = version;

		setModal(true);
		center();
		setHeight(null);
		setWidth(50, UNITS_PERCENTAGE);
		addStyleName("versioninfo");

		((AbstractOrderedLayout) getContent()).setSpacing(true);

		setCaption(AdminMainLayout.ct().get("contenttool.versioninfo.caption")
				+ " " + Util.parseVersion(version));
		setIcon(Util.getResource("icons/information"));

		build();
	}

	private void build() {

		int changes = 0;
		for (Entry<String, Value> e : version.getValuesInThis().entrySet()) {
			for (Entry<Locale, List<Localization>> e2 : e.getValue()
					.getLocalizations().entrySet()) {
				changes += e2.getValue().size();
			}
		}

		Label l = new Label(AdminMainLayout.ct().get(
				"contenttool.versioninfo.numchanges")
				+ " " + changes);
		addComponent(l);

		if (!version.isPreview()) {
			l = new Label(AdminMainLayout.ct().get(
					"contenttool.versioninfo.committed")
					+ " " + df.format(version.getCommitDate()));
			addComponent(l);
			l = new Label(AdminMainLayout.ct().get(
					"contenttool.versioninfo.committer")
					+ " "
					+ AdminMainLayout.ct().conf()
							.getUserNameForAdminUI(version.getCommitterId()));
			addComponent(l);
		}

		showAll = new Button(AdminMainLayout.ct().get(
				"contenttool.versioninfo.showall"));
		showAll.addStyleName(BaseTheme.BUTTON_LINK);
		showAll.setEnabled(changes > 0);
		showAll.addListener(new ClickListener() {

			private static final long serialVersionUID = 6856128403940554163L;

			@Override
			public void buttonClick(ClickEvent event) {
				showAll();
			}
		});
		addComponent(showAll);

		clearAll = new Button(AdminMainLayout.ct().get(
				"contenttool.versioninfo.clearall"));
		clearAll.setVisible(version.isPreview());
		clearAll.setEnabled(changes > 0);
		clearAll.addListener(new ClickListener() {

			private static final long serialVersionUID = 3637726351287932645L;

			@Override
			public void buttonClick(ClickEvent event) {
				confirmClear();
			}
		});
		addComponent(clearAll);
	}

	private void confirmClear() {
		final HorizontalLayout hl = new HorizontalLayout();
		hl.setSpacing(true);

		Button confirm = new Button(AdminMainLayout.ct().get(
				"contenttool.confirm.yes"));
		confirm.setIcon(Util.getResource("icons/accept"));
		hl.addComponent(confirm);
		confirm.addListener(new ClickListener() {

			private static final long serialVersionUID = 8460467854770161208L;

			@Override
			public void buttonClick(ClickEvent event) {
				AdminMainLayout.ct().getModel().discardPreview();
				// re-draw all
				AdminMainLayout.bb().fire(new VersionChangedEvent(version));
				close();
			}
		});

		Button cancel = new Button(AdminMainLayout.ct().get(
				"contenttool.confirm.no"));
		cancel.setIcon(Util.getResource("icons/cancel"));
		hl.addComponent(cancel);
		cancel.addListener(new ClickListener() {

			private static final long serialVersionUID = -7286207512294952210L;

			@Override
			public void buttonClick(ClickEvent event) {
				replaceComponent(hl, clearAll);
			}
		});

		replaceComponent(clearAll, hl);
	}

	private void showAll() {
		setHeight(50, UNITS_PERCENTAGE);
		center();

		GridLayout gl = new GridLayout(5, 1);
		gl.setWidth(100, UNITS_PERCENTAGE);
		gl.setSpacing(true);
		gl.setColumnExpandRatio(2, 1);
		gl.setColumnExpandRatio(4, 1);

		Label l = new Label(AdminMainLayout.ct().get(
				"contenttool.versioninfo.id"));
		l.setSizeUndefined();
		l.addStyleName("gridheader");
		gl.addComponent(l);

		l = new Label(AdminMainLayout.ct().get("contenttool.versioninfo.lang"));
		l.setSizeUndefined();
		l.addStyleName("gridheader");
		gl.addComponent(l);

		l = new Label(AdminMainLayout.ct().get("contenttool.versioninfo.value"));
		l.setSizeUndefined();
		l.addStyleName("gridheader");
		gl.addComponent(l);

		l = new Label(AdminMainLayout.ct().get("contenttool.versioninfo.date"));
		l.setSizeUndefined();
		l.addStyleName("gridheader");
		gl.addComponent(l);

		l = new Label(AdminMainLayout.ct()
				.get("contenttool.versioninfo.oldval"));
		l.setSizeUndefined();
		l.addStyleName("gridheader");
		gl.addComponent(l);

		for (Entry<String, Value> e : version.getValuesInThis().entrySet()) {

			Value prev = e.getValue().getPreviousVersion();

			for (Entry<Locale, List<Localization>> e2 : e.getValue()
					.getLocalizations().entrySet()) {

				for (Localization loc : e2.getValue()) {

					l = new Label(e.getKey());
					l.setSizeUndefined();
					gl.addComponent(l);

					Embedded flag = Util.getFlag(loc.getLanguage().toString());
					gl.addComponent(flag);
					gl.setComponentAlignment(flag, Alignment.MIDDLE_CENTER);

					String value;
					if (e.getValue().isDeleted()) {
						value = AdminMainLayout.ct().get(
								"contenttool.versioninfo.deleted");
					} else {
						value = loc.getValue();
					}
					l = new Label(Util.asHTML(value), Label.CONTENT_XHTML);
					// l.setSizeUndefined();
					gl.addComponent(l);

					String publishDate;
					if (loc.getStartDate() == null) {

						if (version.isPreview()) {
							publishDate = "-";
						} else {
							publishDate = df.format(version.getCommitDate());
						}
					} else {
						publishDate = df.format(loc.getStartDate());
					}

					l = new Label(publishDate);
					l.setSizeUndefined();
					gl.addComponent(l);

					if (prev != null) {
						for (Localization oldVal : prev.getAllLocalizations()
								.get(loc.getLanguage())) {
							l = new Label(oldVal.getValue());
						}
					} else {
						l = new Label("-");
					}
					l.setSizeUndefined();
					gl.addComponent(l);
				}
			}
		}

		replaceComponent(showAll, gl);
	}
}
